package com.situ.web.servlet;

import com.situ.web.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/ajax1")
public class Ajax1Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Ajax1Servlet.doGet");
        String name = req.getParameter("name");
        System.out.println(name);

        /*try {
            Thread.sleep(9000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }*/

        List<Student> list = new ArrayList<>();
        // [{"name":"李四","age":23}, {"name":"李四","age":23}, {}]
        //转义：转换你原来的意思
        // {"name":"李四","age":23}
        //resp.setContentType();
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("{\"name\":\"李四\",\"age\":23}");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Ajax1Servlet.doPost");
        String name = req.getParameter("name");
        System.out.println(name);

        //转义：转换你原来的意思
        // {"name":"李四","age":23}
        //resp.setContentType();
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("{\"name\":\"李四11\",\"age\":23}");
    }
}
